<#--
	_gridModel: 1 datagrid header, 2 the hidden template, 3 body, 9 populate column information
	_gridModel: 4/41/42 pageinfo header, 5 body, 6 advance search template, 9 populate column information(TODO)
	
Width setting:
	total width: controlled by 'style' property
	labelWidth: total width - width - 8 (8 is the space width)
	width: the input width 
-->
<#macro control type id='' name='' value='' defaultValue='' labelValue='undefined' label='' clazz='' style=''
	 width=0 required=false colspan=1 positionCol=-1 show=true textModel=false>
	<#assign pElement=false>
	<#if _namePrefix?? && name != ''>
		<#local fullName = _namePrefix + name>
	<#else>
		<#local fullName = name>
	</#if>
	
	<#-- prepare the initial value-->
	<#assign val=''>
	<#assign isNewPE = false>
	<#if ps?? && name != '' && (!_gridModel?? || _gridModel != 6)> <#-- special logic for the search template -->
		<#assign pElement = ps.getElement(fullName, false)!false>
		<#if pElement?is_boolean>
			<#-- populate ps element -->
			<#assign pElement = ps.addElement(type, fullName, label, required, textModel, width, show)>
			<#assign isNewPE = true>
		</#if>
		
		<#assign pid=pElement.id!id>
		<#assign pcolspan=pElement.colspan!colspan>
		<#assign plabel=pElement.label!label>
		<#assign pclazz=pElement.clazz!clazz>
		<#assign ptextModel=pElement.textModel!textModel>
		<#if pElement.length?? && pElement.length gt 0>
			<#assign pwidth=pElement.length>
		<#else>
			<#assign pwidth=width>
		</#if>
		<#if pElement.hidden??>
			<#assign pshow=!pElement.hidden>
		<#else>
			<#assign pshow=show>
		</#if>
		<#if !ptextModel && pshow>
			<#assign prequired=pElement.required!prequired>
		<#else>
			<#assign prequired=false>
		</#if>
	<#else>
		<#assign pElement = ps.newElement()>
		<#if (_gridModel == 6)!false>		
			<#local textModel = false>
		</#if>
		
		<#assign pid=id>
		<#assign pcolspan=colspan>
		<#assign plabel=label>
		<#assign pclazz=clazz>
		<#assign ptextModel=textModel>
		<#assign pshow=show>
		<#assign pwidth=width>
		<#if !ptextModel && pshow>
			<#assign prequired=required>
		<#else>
			<#assign prequired=false>
		</#if>
	</#if>
	<#-- end prepare initial value -->
	
	<#-- 0, normal case, 6, build the search template, used by jpageinfo.ftl -->
	<#if !_gridModel?? || _gridModel == 0 || _gridModel == 6>
		<#if value?is_string && value=='' && name != '' && name?substring(0, 1) != '_'>
			<#assign val=_getValue(name)>
		<#else>
			<#assign val=value>
		</#if>
		<#if defaultValue != '' && value?is_string && value == ''>
			<#assign val=defaultValue>
		</#if>
		
		<#local name = fullName>
		<#if _gridModel?? && _gridModel == 6>
			<#assign ptextModel = false>
			<#assign pwidth = 0>
			<#local style = ''>
		</#if>
		<#if !pshow>
			<span class="stdEle hide"<#if positionCol != -1> positionCol='${positionCol}'</#if><#if style!=''> style='${style}'</#if>><#nested></span>
		<#elseif !tag.isFormItemStyle(type)>
			<#nested><#t>
		<#else>
			<#--FIXME: control the width here? -->
			<#if plabel==''>
				<#if ptextModel>
					<#local sval=fn.s(val, name, false)/>
					<#if labelValue != 'undefined'><#local sdval=fn.s(labelValue,'',false)/><#else><#local sdval=sval/></#if>
					<div class="j-form-item noLabel<#if style!=''> customStyle</#if> ${pclazz} stdEle" uiType="${type}"<#if style!='' || pwidth!=0> style='<#if pwidth!=0>width:${pwidth}px;</#if>${style}'</#if><#if positionCol != -1> positionCol='${positionCol}'</#if><#if pid!=''> id="${pid}"</#if>><#t>
						<span txtName="${name}" title="${fn.quote(sdval)}">${fn.escape(sdval, name)}<#t>
					</span><#t>
					<input type="hidden" name="${name}" value="${fn.quote(sval)}"></div><#t>
				<#else>
					<div class="j-form-item noLabel<#if style!=''> customStyle</#if> ${pclazz} stdEle" uiType="${type}"<#if style!='' || pwidth!=0> style='<#if pwidth!=0>width:${pwidth}px;</#if>${style}'</#if><#if positionCol != -1> positionCol='${positionCol}'</#if><#if pid!=''> id="${pid}"</#if>><#t>
						<#nested><#t>
					</div><#t>
				</#if>
			<#else>
				<#if style != ''>
					<#if pwidth != 0>
						<#local totalItemWidth = tag.css(0, style, 'width')>
						<#if totalItemWidth == 0>
							<#local totalItemWidth = 300>
						</#if>
						<#local labelWidth = totalItemWidth - pwidth - 8>
					</#if>
					<#local color = tag.css(style, 'color')>
				</#if>
				<div class="j-form-item<#if style!=''> customStyle</#if> ${pclazz} stdEle" uiType="${type}"<#if style!=''> style='${style}'</#if><#if pcolspan gt 1> colspan=${pcolspan}</#if><#if positionCol != -1> positionCol='${positionCol}'</#if><#if pid!=''> id="${pid}"</#if>>
					<label class="label" <#if labelWidth?exists || color?exists && color != ''>style="<#if labelWidth?exists>width:${labelWidth}px;</#if><#if color?exists>${color};</#if>"</#if>><#rt>
						<#if prequired><font color="red">*</font></#if><#t>
						<#if plabel != ''><@i18n name=plabel/>:</#if><#t>
					</label><#lt>
					<span class="span"<#if pwidth!=0 || labelWidth?exists> style="<#if pwidth!=0>width:${pwidth}px;</#if><#if labelWidth?exists>margin-left:${labelWidth+8}px;float:none</#if>"</#if>><#rt>
					<#if ptextModel>
						<#local sval=fn.s(val, name, false)/>
						<#if !(labelValue?is_string && labelValue == 'undefined')><#local sdval=fn.s(labelValue,'',false)/><#else><#local sdval=sval/></#if>
						<span txtName="${name}" class="readText" title="${tag.formatTitle(sdval)}">${fn.escape(sdval, name)}</span><#t>
						<input type="hidden" name="${name}" value="${fn.quote(sval)}"><#t>
					<#else><#t>
						<#nested><#t>
					</#if><#t>
					</span>
				</div>
			</#if>
		</#if>
	<#elseif _gridModel==1 || _gridModel==11>
		<#-- only show the label, used by jdatagrid.ftl -->
		<#if pshow>
			<th name="${name}" class='${pclazz} stdEle' fullName="${_namePrefix+name}"<#t>
					<#if pwidth gt 0 || style!=''> style="<#if pwidth gt 0>width:${pwidth}px;</#if>${style}"</#if>>
				<#if _gridModel==1>
					<div class="th"><div class="label"><#if prequired><font color="red">*</font></#if><@i18n name=plabel/></div></div>
				<#else>
					<#assign _oldNamePrefix = _namePrefix>
					<#assign _namePrefix = ''>
					<#nested>
					<#assign _namePrefix = _oldNamePrefix>
				</#if>
			</th>
		</#if>
	<#elseif _gridModel==2>
		<#-- only show the tag (empty value), used by template of jdatagrid.ftl -->
		<#if pshow>
			<#assign val=defaultValue>
			<td<#if pclazz!=''> class='${pclazz}'</#if>>
				<#if ptextModel>
					<span txtName="${name}" class='readonly'></span>
				<#else>
					<#assign _oldNamePrefix = _namePrefix>
					<#assign _namePrefix = ''>
					<#nested>
					<#assign _namePrefix = _oldNamePrefix>
				</#if>
			</td>
		</#if>
	<#elseif _gridModel==3 || _gridModel==5>
		<#-- show the tag (with value), table body cell, used by jdatagrid.ftl(3), jpageinfo.ftl(5) -->
		<#if pshow>
			<#if labelValue?string == 'undefined' && value?string==''>
				<#assign val=fn.val(_gridItem, name, _namePrefix + name)!''>
			<#else>
				<#assign val=value>
			</#if>
			<#if defaultValue != '' && value?is_string && value == ''>
				<#assign val=defaultValue>
			</#if>
			
			<td<#if pclazz!=''> class='${pclazz}'</#if>>
				<#if ptextModel>
					<#if _gridModel==3>
						<div txtName='${name}' class='readonly'><#if labelValue != 'undefined'>${labelValue}<#else>${fn.escape(val, name)}</#if></span></div>
						<input type="hidden" name="${name}" value="${fn.s(val, name)}">
					<#else>
						<#if labelValue != 'undefined'>
							<#assign val=labelValue>
						</#if>
						<div txtName='${name}' class="td ellipsis">${fn.escape(val, name)}</div>
					</#if>
				<#else>
					<#if _gridModel==3>
						<#-- don't output the _namePrefix -->
						<#assign _oldNamePrefix = _namePrefix>
						<#assign _namePrefix = ''>
						<#nested>
						<#assign _namePrefix = _oldNamePrefix>
					<#else>
						<div class="td" txtName='${name}'><#nested></div>
					</#if>
				</#if>
			</td>
		</#if>
	<#elseif _gridModel==4 || _gridModel==41 || _gridModel==42>
		<#-- pageinfo title, show the label with sort and filter flag, used by jpageinfo.ftl -->
		<#-- 4: show the sort flag , 41 & 42: don't show the sort flag, 42: use nested content-->
		<#if pshow>
			<#assign _colIndex = _colIndex + 1>
			<#if style != '' && pwidth == 0>
				<#assign pwidth = tag.css(0, style, 'width')>
			</#if>
			
			<th class="${pclazz} stdEle" name="${name}" fullName="${_namePrefix+name}"<#t> uiType="${type}"
				<#if _emptyColWidthCount == 0 && (_colIndex + 1) == _colCount>
					<#local style = tag.mergeCssWidth(style, 0)>
				<#else>
					<#local style = tag.mergeCssWidth(style, pwidth)>
				</#if>
				<#if style!=''> style="${style}"</#if>>
				<div class="th"><#t>
					<#if _gridModel==4><a href="javascript:void(0)" sort="${name}" dir="ASC" onclick="sortSearch(this, event)"></#if><#t>
					<div class="label ellipsis"><#t>
						<#if _gridModel==42><#t>
							<#nested><#t>
						<#else><#t>
							<@i18n name=plabel/><#t>
						</#if><#t>
					</div><#t>
					<#if _gridModel==4></a></#if><#t>
				</div><#t>
			</th>
		</#if>
	<#elseif _gridModel==9>
		<#-- datagrid, pageinfo, don't output any, just populate the page element information -->
	</#if>
</#macro>